package de.jmda.mview.proj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.apache.log4j.Logger;

public class DiagramsTreeDiagramGroupPopup implements ActionListener
{
	private final static Logger LOGGER =
      Logger.getLogger(DiagramsTreeDiagramGroupPopup.class);

	/**
	 * Enumerates the menu items for the popup.
	 */
	private enum Items
	{
		RENAME(       "rename"),
		NEW_GROUP(    "new group"),
		NEW_SUB_GROUP("new sub group"),
		NEW_DIAGRAM(  "new diagram"),
		DELETE(       "delete"),
		;

		private String name;

		private Items(String name)
		{
			this.name = name;
		}
	}

	/**
	 * event that represents the tree node click
	 */
	private DiagramsTreeNodeClicked diagramsTreeNodeClicked;

	/**
	 * diagram group of the clicked tree node
	 */
	private DiagramGroup diagramGroup;

	/**
	 * shortcut to {@link DiagramsTreeNodeClicked#getDiagramsController()}
	 */
	private DiagramsController diagramsController;
	
	/**
	 * shortcut to {@link DiagramsTreeNodeClicked#getMouseEvent()}
	 */
	private MouseEvent mouseEvent;

	private JPopupMenu menu;

	/**
	 * @param diagramsTreeEventNodeClicked2
	 * @param diagramGroup
	 */
	public DiagramsTreeDiagramGroupPopup(
      DiagramsTreeNodeClicked diagramsTreeNodeClicked,
      DiagramGroup diagramGroup)
  {
		super();

		this.diagramsTreeNodeClicked = diagramsTreeNodeClicked;
		this.diagramGroup = diagramGroup;
		
		diagramsController = diagramsTreeNodeClicked.getDiagramsController();
		mouseEvent = diagramsTreeNodeClicked.getMouseEvent();
		
		menu = initialiseMenu();
  }

	private JPopupMenu initialiseMenu()
  {
		menu = new JPopupMenu();

		JMenuItem itemRename = new JMenuItem(Items.RENAME.name);
		itemRename.addActionListener(this);
		menu.add(itemRename);
		
		JMenuItem itemNewGroup = new JMenuItem(Items.NEW_GROUP.name);
		itemNewGroup.addActionListener(this);
		menu.add(itemNewGroup);

		JMenuItem itemNewSubGroup = new JMenuItem(Items.NEW_SUB_GROUP.name);
		itemNewSubGroup.addActionListener(this);
		menu.add(itemNewSubGroup);

		JMenuItem itemNewDiagram = new JMenuItem(Items.NEW_DIAGRAM.name);
		itemNewDiagram.addActionListener(this);
		menu.add(itemNewDiagram);

		JMenuItem itemDelete = new JMenuItem(Items.DELETE.name);
		itemDelete.addActionListener(this);
		menu.add(itemDelete);
		
		return menu;
  }

	public void run()
	{
		menu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent)
	{
		if (actionEvent.getActionCommand().equals(Items.RENAME.name))
		{
			rename();
		}
		else if (actionEvent.getActionCommand().equals(Items.NEW_GROUP.name))
		{
			newGroup();
		}
		else if (actionEvent.getActionCommand().equals(Items.NEW_SUB_GROUP.name))
		{
			newSubGroup();
		}
		else if (actionEvent.getActionCommand().equals(Items.NEW_DIAGRAM.name))
		{
			newDiagram();
		}
		else if (actionEvent.getActionCommand().equals(Items.DELETE.name))
		{
			delete();
		}
		else
		{
			LOGGER.warn(
					"unexpected action event with action command " +
					actionEvent.getActionCommand());
		}
	}

	private void rename()
  {
		diagramsController.rename(diagramsTreeNodeClicked.getTreePath());
  }

	private void newGroup()
  {
		diagramsController.newDiagramGroup();
  }

	private void newSubGroup()
  {
		diagramsController.newDiagramSubGroup(diagramGroup);
  }

	private void newDiagram()
  {
		diagramsController.newDiagram(diagramGroup);
  }

	private void delete()
  {
		diagramsController.deleteDiagramGroup(diagramGroup);
  }
}